package com.daisuxia.mg.utils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

//<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class HttpUtils
{
  
 
  
  public static void main(String[] args) {}
  
  public static String postJsonData(String url, String jsonStrData)
  {
    //<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->.info("# POST JSON 请求URL 为" + url);
    //<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->.info("# POST JSON 数据为" + jsonStrData);
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    HttpPost post = new HttpPost(url);
    String jsonObject = null;
    CloseableHttpClient closeableHttpClient = null;
    try
    {
      closeableHttpClient = httpClientBuilder.build();
      HttpEntity entity = new StringEntity(jsonStrData);
      new StringEntity(jsonStrData);
      new StringEntity(jsonStrData, "UTF-8");
      post.setEntity(entity);
      post.setHeader("Content-type", "application/json");
      HttpResponse resp = closeableHttpClient.execute(post);
      jsonObject = convertResponseBytes2JsonObj(resp);
    }
    catch (IOException e)
    {
      e.printStackTrace();
      if (null != closeableHttpClient) {
        try
        {
          closeableHttpClient.close();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
    }
    return jsonObject;
  }
  
  public static String  getJsonResponse(String url)
  {
    //<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->.info("# GET 请求URL 为" + url);
	  String jsonObject = null ;
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    HttpGet get = new HttpGet(url);
    CloseableHttpClient closeableHttpClient = null;
    try
    {
      closeableHttpClient = httpClientBuilder.build();
      HttpResponse resp = closeableHttpClient.execute(get);
      jsonObject = convertResponseBytes2JsonObj(resp);
    }
    catch (IOException e)
    {
      e.printStackTrace();
      if (null != closeableHttpClient) {
        try
        {
          closeableHttpClient.close();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
    }
    return jsonObject;
  }
  
  private static String convertResponseBytes2JsonObj(HttpResponse resp)
  {
	  String result = null ;
    try
    {
      InputStream respIs = resp.getEntity().getContent();
      byte[] respBytes = IOUtils.toByteArray(respIs);
       result = new String(respBytes, Charset.forName("UTF-8"));
      if ((null == result) || (result.length() == 0))
      {
        System.err.println("无响应");
      }
      else
      {
        System.out.println(result);
        if ((result.startsWith("{")) && (result.endsWith("}"))) {
        	
        } else {
          System.err.println("不能转成JSON对象");
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return result;
  }
}
